package com.k2data.k2app.controller;

import com.github.pagehelper.PageHelper;
import com.k2data.k2app.constant.Constant;
import com.k2data.k2app.domain.po.TaskDTO;
import com.k2data.k2app.response.CommonResultResponse;
import com.k2data.k2app.response.PageInfo;
import com.k2data.k2app.response.ResponseHelper;
import com.k2data.k2app.service.TaskService;
import com.k2data.k2app.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <pre>
 * 分析任务
 * Created by clb on 17-3-17.
 * </pre>
 */
@Api(description = "分析任务")
@RestController
@RequestMapping("/tasks")
public class TaskController {
    @Autowired
    private TaskService taskService;

    @ApiOperation(value = "创建")
    @ApiImplicitParam(name = "deviceHealthTask", value = "deviceHealthTask对象", required = true, dataType = "DeviceHealthTask", paramType = "body")
    @PostMapping(value = "", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<CommonResultResponse> post(@RequestBody TaskDTO taskDO) throws Exception {
        return ResponseHelper.success(taskService.create(taskDO));
    }

    @ApiOperation(value = "更新")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "deviceHealthTask", value = "deviceHealthTask对象", required = true, dataType = "DeviceHealthTask", paramType = "body")
    })
    @PutMapping(value = "/{id}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<CommonResultResponse> update(@PathVariable("id") Long id,
                                                       @RequestBody TaskDTO taskDO) throws Exception {
        taskDO.setId(id);
        return ResponseHelper.success(taskService.updateTask(taskDO));
    }

    @ApiOperation(value = "查询(带分页)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小(默认 30)", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "taskName", value = "按照任务名称,模糊查询", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "modelId", value = "模型id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "taskType", value = "任务类型", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "deviceType", value = "设备类型", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "customer", value = "客户", dataType = "Long", paramType = "query")
    })
    @GetMapping(value = "")
    public ResponseEntity<CommonResultResponse> page(@RequestParam(value = "pageNum", required = false) Integer pageNum,
                                                     @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                                     @RequestParam(value = "taskName", required = false) String taskName,
                                                     @RequestParam(value = "deviceId", required = false) Long deviceId,
                                                     @RequestParam(value = "modelId", required = false) Long modelId,
                                                     @RequestParam(value = "taskType", required = false) String taskType,
                                                     @RequestParam(value = "deviceType", required = false) String deviceType,
                                                     @RequestParam(value = "customer", required = false) Long customer) throws Exception {
        TaskDTO taskDTO = new TaskDTO();
        if(!StringUtils.isEmpty(taskName)){
            taskDTO.setTaskName(taskName);
        }
        if(!StringUtils.isEmpty(taskType)){
            taskDTO.setTaskType(taskType);
        }
        if(!StringUtils.isEmpty(deviceType)){
            taskDTO.setDeviceType(deviceType);
        }
        taskDTO.setDeviceId(deviceId);
        taskDTO.setCustomerId(customer);
        taskDTO.setModelId(modelId);
        pageSize = pageSize == null ? Constant.DEFAULT_PAGE_SIZE : pageSize;
        pageNum = (pageNum == null) ? Integer.valueOf(1) : pageNum;
        int count = taskService.selectCount(taskDTO);
        PageHelper.startPage(pageNum, pageSize == null ? Constant.DEFAULT_PAGE_SIZE : pageSize);
        PageInfo pageInfo = new PageInfo(pageNum, pageSize,count);
        List<TaskDTO> list = taskService.listWithDevice(taskDTO,false);
        return ResponseHelper.success(list,pageInfo);
    }

    @ApiOperation(value = "根据id获取")
    @ApiImplicitParam(name = "id", value = "the value of id", required = true, dataType = "String", paramType = "path")
    @GetMapping(value = "/{id}")
    public ResponseEntity<CommonResultResponse> get(@PathVariable("id") String id) throws Exception {
        return ResponseHelper.success(taskService.get(Long.parseLong(id)));
    }

    @ApiOperation(value = "删除(包括关联的设备/作业/关联关系)")
    @ApiImplicitParam(name = "id", value = "任务id", required = true, dataType = "Long", paramType = "path")
    @DeleteMapping(value = "/{id}")
    public ResponseEntity<CommonResultResponse> delete(@PathVariable("id") Long id) throws Exception {
        return ResponseHelper.success(taskService.delete(id));
    }

    @ApiOperation(value = "启动任务")
    @ApiImplicitParam(name = "taskId", value = "任务id", required = true, dataType = "Long", paramType = "path")
    @GetMapping(value = "/{taskId}/run")
    public ResponseEntity<CommonResultResponse> run(@PathVariable Long taskId) throws Exception {
        return ResponseHelper.success(taskService.run(taskId,"manual"));
    }

}
